package com.easylinkin.linkappapi.crane.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.crane.entity.CraneOperationArea;
import com.easylinkin.linkappapi.crane.entity.CraneVerticalProtectionArea;
import com.easylinkin.linkappapi.crane.service.ICraneVerticalProtectionAreaService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 吊车垂直面保护区表 前端控制器
 *
 * @author xy
 * @since 2025-06-16
 */
@Api(tags = "吊车垂直面保护区表")
@RestController
@RequestMapping("/crane/craneVerticalProtectionArea")
public class CraneVerticalProtectionAreaController {
    @Autowired
    private ICraneVerticalProtectionAreaService craneVerticalProtectionAreaService;
    @Autowired
    private LinkappUserContextProducer linkappUserContextProducer;


    /**
     * 分页列表查询
     *
     * @param requestModel
     * @return
     */
    @ApiOperation(value="分页列表查询", notes="分页列表查询")
    @PostMapping(value = "/listPage")
    public RestMessage queryPageList(@RequestBody RequestModel<CraneVerticalProtectionArea> requestModel) {
        // Assert.notNull(requestModel,"参数不能为空");
        // Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<CraneVerticalProtectionArea> pageList = craneVerticalProtectionAreaService.queryPageList(requestModel);
        return RestBuilders.successBuilder().data(pageList).build();
    }

    /**
     * 保存
     *
     * @param craneVerticalProtectionArea
     * @return
     */
    @ApiOperation(value="保存", notes="保存")
    @PostMapping(value = "/save")
    public RestMessage save(@RequestBody CraneVerticalProtectionArea craneVerticalProtectionArea) {
        setBase(craneVerticalProtectionArea);
        if(craneVerticalProtectionArea.getId() != null){
            craneVerticalProtectionAreaService.updateById(craneVerticalProtectionArea);
        } else{
            //新增默认关闭
            craneVerticalProtectionArea.setEnabledFlag(0);
            craneVerticalProtectionAreaService.save(craneVerticalProtectionArea);
        }
        // TODO 计算告警
        return RestBuilders.successBuilder().message("保存成功" ).build();
    }

    /**
     * 设置基本属性
     *
     * @param craneVerticalProtectionArea
     */
    private void setBase(CraneVerticalProtectionArea craneVerticalProtectionArea) {
        // 名称唯一校验
        LambdaQueryWrapper<CraneVerticalProtectionArea> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CraneVerticalProtectionArea::getRegionName, craneVerticalProtectionArea.getRegionName());
        queryWrapper.eq(CraneVerticalProtectionArea::getTenantId, linkappUserContextProducer.getNotNullCurrent().getTenantId());
        if (craneVerticalProtectionArea.getId() != null) {
            queryWrapper.ne(CraneVerticalProtectionArea::getId, craneVerticalProtectionArea.getId());
        }
        Integer count = craneVerticalProtectionAreaService.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException("区域名称已存在");
        }
        // 工点唯一性校验
        LambdaQueryWrapper<CraneVerticalProtectionArea> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(CraneVerticalProtectionArea::getWorkStationId, craneVerticalProtectionArea.getWorkStationId());
        queryWrapper1.eq(CraneVerticalProtectionArea::getTenantId, linkappUserContextProducer.getNotNullCurrent().getTenantId());
        if (craneVerticalProtectionArea.getId() != null) {
            queryWrapper1.ne(CraneVerticalProtectionArea::getId, craneVerticalProtectionArea.getId());
        }
        Integer count1 = craneVerticalProtectionAreaService.count(queryWrapper1);
        if (count1 > 0) {
            throw new BusinessException("工点已存在");
        }
        // 设置基本属性
        craneVerticalProtectionArea.setModifyTime(new Date());
        craneVerticalProtectionArea.setModifyId(linkappUserContextProducer.getCurrent().getId());
        //没有id就是新增,有就是编辑
        if (null == craneVerticalProtectionArea.getId()) {
            craneVerticalProtectionArea.setCreateId(linkappUserContextProducer.getCurrent().getId());
            craneVerticalProtectionArea.setCreateTime(new Date());
            craneVerticalProtectionArea.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        }
    }

    /**
     * 启用/禁用
     *
     * @param id
     * @param enabledFlag
     * @return
     */
    @ApiOperation(value="启用/禁用", notes="启用/禁用")
    @PostMapping(value = "/enableOrDisable")
    public RestMessage enableOrDisable(@RequestParam("id") Integer id, @RequestParam("enabledFlag") Integer enabledFlag) {
        CraneVerticalProtectionArea craneVerticalProtectionArea = craneVerticalProtectionAreaService.getById(id);
        if(craneVerticalProtectionArea == null) {
            return RestBuilders.errorBuilder().message("未找到对应数据").build();
        }
        craneVerticalProtectionArea.setEnabledFlag(enabledFlag);
        craneVerticalProtectionAreaService.updateById(craneVerticalProtectionArea);
        return RestBuilders.successBuilder().message("操作成功").build();
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value="通过id删除", notes="通过id删除")
    @DeleteMapping(value = "/delete")
    public RestMessage delete(@RequestParam("id") Integer id) {
        craneVerticalProtectionAreaService.removeById(id);
        return RestBuilders.successBuilder().message("删除成功").build();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public RestMessage deleteBatch(@RequestParam("ids") String ids) {
        this.craneVerticalProtectionAreaService.removeByIds(Arrays.asList(ids.split(",")));
        return RestBuilders.successBuilder().message("批量删除成功").build();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public RestMessage queryById(@RequestParam("id") Integer id) {
        CraneVerticalProtectionArea craneVerticalProtectionArea = craneVerticalProtectionAreaService.getById(id);
        if(craneVerticalProtectionArea == null) {
            return RestBuilders.errorBuilder().message("未找到对应数据").build();
        }
        return RestBuilders.successBuilder().data(craneVerticalProtectionArea).build();
    }
}
